﻿Public Module Compartido
    Public Const Ancho As Integer = 10
    Public Const Alto As Integer = 10
    Public Const BarcosDeUnaCasilla As Integer = 4
    Public Const BarcosDeDosCasillas As Integer = 3
    Public Const BarcosDeTresCasillas As Integer = 2
    Public Const BarcosDeCuatroCasillas As Integer = 1
    Public Const TiposDiferentesDeBarcos As Integer = 4
#If DEBUG Then
    Public Const SigueHaciendoPing As Boolean = True
#Else
    Public Const SigueHaciendoPing As Boolean = false
#End If
    Private ReadOnly R As Random
    Private ReadOnly P2PWrapper As P2PWrapper
    Private _númeroTicketTurno As Integer = -1

    Sub New()
        R = New Random(Now.Millisecond)
        P2PWrapper = New P2PWrapper()
    End Sub

    Public ReadOnly Property Título
        Get
            Return String.Format("{0} - {1}", "Pequeño juego de barcos", P2PWrapper.HostPropio.UserName)
        End Get
    End Property

    Public ReadOnly Property P2PEncapsulado() As P2PWrapper
        Get
            Return P2PWrapper
        End Get
    End Property

    Public ReadOnly Property TotalDeBarcos As Integer
        Get
            Return BarcosDeUnaCasilla + BarcosDeDosCasillas + BarcosDeTresCasillas + BarcosDeCuatroCasillas
        End Get
    End Property

    Public ReadOnly Property NúmeroTicketTurno As Integer
        Get
            If _númeroTicketTurno < 0 Then
                _númeroTicketTurno = UnRandom(1000)
            End If
            Return _númeroTicketTurno
        End Get
    End Property

    Public Function UnRandom(ByVal maximo As Integer) As Integer
        Return R.Next(maximo)
    End Function

    Public Function UnaPosicionAlAzar() As Point
        Return New Point(UnRandom(Ancho), UnRandom(Alto))
    End Function

    Public Function CreaHashDisparo(ByVal destino As HostHelper, ByVal posicion As Point) As String
        Return String.Format("{0} {1:0} X{2:00}Y{3:00}", destino.IDHost, Convert.ToInt32(TipoMensaje.Disparo), posicion.X.ToString("00"), posicion.Y.ToString("00"))
    End Function

    Public Function CreaHashInfoImpacto(destino As HostHelper, posicion As Point) As String
        Return String.Format("{0} {1:0} X{2:00}Y{3:00}", destino.IDHost, Convert.ToInt32(TipoMensaje.ImpactoAgua), posicion.X.ToString("00"), posicion.Y.ToString("00"))
    End Function

    Public Function CreaHashInfoImpacto(ByVal destino As HostHelper, ByVal posicion As Point, idBarco As Integer, orientacion As TipoOrientación, tipo As TipoBarco, subParte As Integer) As String
        Return String.Format("{0} {1:0} X{2:00}Y{3:00}I{4:00}{5}{6:0}{7:0}", destino.IDHost, Convert.ToInt32(TipoMensaje.ImpactoBarco), posicion.X.ToString("00"), posicion.Y.ToString("00"), idBarco.ToString("00"), orientacion.ToString.Substring(0, 1), Convert.ToInt32(tipo), subParte.ToString("0"))
    End Function
End Module